package com.example.transitionlearning;


import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.transition.Scene;
import android.transition.Transition;
import android.transition.TransitionInflater;
import android.transition.TransitionManager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

public class MainActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (savedInstanceState == null) {
      getFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
    }
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
      return true;
    }
    return super.onOptionsItemSelected(item);
  }

  /**
   * A placeholder fragment containing a simple view.
   */
  public static class PlaceholderFragment extends Fragment {
    ViewGroup viewScene2;
    Scene scene1;
    Scene scene2;
    boolean flag=false;

    public PlaceholderFragment() {
      setHasOptionsMenu(true);
    }
    
    /* (non-Javadoc)
     * @see android.app.Fragment#onCreateOptionsMenu(android.view.Menu, android.view.MenuInflater)
     */
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
      super.onCreateOptionsMenu(menu, inflater);
      menu.add("change").setOnMenuItemClickListener(new OnMenuItemClickListener() {
        
        @Override
        public boolean onMenuItemClick(MenuItem item) {
          Transition my=TransitionInflater.from(getActivity()).inflateTransition(R.transition.my_transition_2);
          if(flag){
            TransitionManager.go(scene1,my);
          }else{
            TransitionManager.go(scene2,my);
          }
          flag=!flag;
//          TransitionManager.beginDelayedTransition(scene2);
          return true;
        }
      });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      View rootView = inflater.inflate(R.layout.fragment_main, container, false);
      return rootView;
    }
    
    /* (non-Javadoc)
     * @see android.app.Fragment#onResume()
     */
    @Override
    public void onResume() {
      super.onResume();
      ListView a = new ListView(getActivity());
      scene1 = Scene.getSceneForLayout((ViewGroup) getView(), R.layout.scene1, getActivity());
      TransitionManager.go(scene1);
      scene2=Scene.getSceneForLayout((ViewGroup) getView(), R.layout.scene2, getActivity());
      viewScene2 = (ViewGroup) getActivity().getLayoutInflater().inflate(R.layout.scene2, null);
    }
  }
}
